﻿using System;
namespace Producers.Kis.Xtee.Riik.Ee.Producer.Kis {
    
    
    [global::Xtee.Core.Attributes.XmlTypeAttribute(Name="CollateralClaim", Namespace="http://producers.kis.xtee.riik.ee/producer/kis", Target=typeof(CollateralClaim))]
    public class CollateralClaimSerializer : global::Xtee.Core.Types.AbstractXteeSerializable {
        
        protected ICollateralClaim clazz;
        
        public CollateralClaimSerializer(ICollateralClaim clazz) {
            this.clazz = clazz;
        }
        
        public override void WriteXml(global::Xtee.Core.IXteeXmlWriter Writer) {
            base.WriteXml(Writer);
            Writer.WriteStartElement("CollateralClaimType");
            if (clazz.CollateralClaimType!=null) {
                Writer.WriteTypeAttribute("http://producers.kis.xtee.riik.ee/producer/kis", "ClaimType");
                Writer.WriteString(clazz.CollateralClaimType.ToString());
            }
            else {
                throw new global::Xtee.Core.Exceptions.NillableNotAllowedException("Nullable is not allowed for element : CollateralClaimType");
            }
            Writer.WriteEndElement();
            if (clazz.CollateralClaimTotal!=null) {
                Writer.WriteStartElement("CollateralClaimTotal");
                Writer.WriteTypeAttribute("http://www.w3.org/2001/XMLSchema", "decimal");
                Writer.WriteString(new global::Xtee.Core.Schema.XsdDecimal(((decimal)(clazz.CollateralClaimTotal))).ToString());
                Writer.WriteEndElement();
            }
            if (clazz.CollateralClaimSpecification!=null) {
                Writer.WriteStartElement("CollateralClaimSpecification");
                global::Xtee.Core.SerializationMetadata metaCollateralClaimSpecification = this.GetMetaData(clazz.CollateralClaimSpecification);
                Writer.WriteTypeAttribute(metaCollateralClaimSpecification.TypeQualifiedName.Namespace, metaCollateralClaimSpecification.TypeQualifiedName.Name);
                metaCollateralClaimSpecification.NewSerializer(clazz.CollateralClaimSpecification).WriteXml(Writer);
                Writer.WriteEndElement();
            }
            for (int x = 0; ((clazz.Dept != null) 
                        && (x < clazz.Dept.Count)); x = (x + 1)) {
                if (clazz.Dept[x]!=null) {
                    Writer.WriteStartElement("Dept");
                    global::Xtee.Core.SerializationMetadata metaDept = this.GetMetaData(clazz.Dept[x]);
                    Writer.WriteTypeAttribute(metaDept.TypeQualifiedName.Namespace, metaDept.TypeQualifiedName.Name);
                    metaDept.NewSerializer(clazz.Dept[x]).WriteXml(Writer);
                    Writer.WriteEndElement();
                }
            }
            if (clazz.CollateralClaimTypeDescription!=null) {
                Writer.WriteStartElement("CollateralClaimTypeDescription");
                Writer.WriteTypeAttribute("http://producers.kis.xtee.riik.ee/producer/kis", "NonEmptyString");
                Writer.WriteString(clazz.CollateralClaimTypeDescription.ToString());
                Writer.WriteEndElement();
            }
        }
        
        public override void ReadXml(global::Xtee.Core.IXteeXmlReader Reader) {
            base.ReadXml(Reader);
            if (Reader.LocalName=="CollateralClaimType") {
                if (Reader.IsElementNil()) {
                    throw new global::Xtee.Core.Exceptions.NillableNotAllowedException(">>CollateralClaim>CollateralClaimType");
                }
                clazz.CollateralClaimType = new global::Producers.Kis.Xtee.Riik.Ee.Producer.Kis.ClaimType(Reader.ReadElementString("CollateralClaimType"));
            }
            else {
                throw new global::Xtee.Core.Exceptions.UnExpectedElementException(">>CollateralClaim>CollateralClaimType", Reader.LocalName);
            }
            if (Reader.LocalName=="CollateralClaimTotal") {
                if (Reader.IsElementNil()) {
                    throw new global::Xtee.Core.Exceptions.NillableNotAllowedException(">>CollateralClaim>CollateralClaimTotal");
                }
                clazz.CollateralClaimTotal = new global::Xtee.Core.Schema.XsdDecimal(Reader.ReadElementString("CollateralClaimTotal")).Value;
            }
            if (Reader.LocalName=="CollateralClaimSpecification") {
                if (Reader.IsElementNil()) {
                    throw new global::Xtee.Core.Exceptions.NillableNotAllowedException(">>CollateralClaim>CollateralClaimSpecification");
                }
                global::Xtee.Core.SerializationMetadata metaCollateralClaimSpecification = this.GetMetaData<global::Producers.Kis.Xtee.Riik.Ee.Producer.Kis.IClaimSpecification>(Reader, "ClaimSpecification", "http://producers.kis.xtee.riik.ee/producer/kis");
                clazz.CollateralClaimSpecification = metaCollateralClaimSpecification.NewEntity<global::Producers.Kis.Xtee.Riik.Ee.Producer.Kis.IClaimSpecification>();
                if ((Reader.IsEmptyElement == false)) {
                    Reader.ReadStartElement("CollateralClaimSpecification");
                    metaCollateralClaimSpecification.NewSerializer(clazz.CollateralClaimSpecification).ReadXml(Reader);
                    Reader.ReadEndElement();
                }
                else {
                    Reader.Skip();
                }
            }
            if (Reader.LocalName=="Dept") {
                clazz.Dept = new System.Collections.Generic.List<global::Producers.Kis.Xtee.Riik.Ee.Producer.Kis.IDept>();
                for (; ((Reader.NodeType != System.Xml.XmlNodeType.EndElement) 
                            && (Reader.LocalName == "Dept")); ) {
                    if (Reader.IsElementNil()) {
                        throw new global::Xtee.Core.Exceptions.NillableNotAllowedException(">>CollateralClaim>Dept");
                    }
                    global::Xtee.Core.SerializationMetadata metaDept = this.GetMetaData<global::Producers.Kis.Xtee.Riik.Ee.Producer.Kis.IDept>(Reader, "Dept", "http://producers.kis.xtee.riik.ee/producer/kis");
                    global::Producers.Kis.Xtee.Riik.Ee.Producer.Kis.IDept item = metaDept.NewEntity<global::Producers.Kis.Xtee.Riik.Ee.Producer.Kis.IDept>();
                    clazz.Dept.Add(item);
                    if ((Reader.IsEmptyElement == false)) {
                        Reader.ReadStartElement("Dept");
                        metaDept.NewSerializer(item).ReadXml(Reader);
                        Reader.ReadEndElement();
                    }
                    else {
                        Reader.Skip();
                    }
                }
            }
            if (Reader.LocalName=="CollateralClaimTypeDescription") {
                if (Reader.IsElementNil()) {
                    throw new global::Xtee.Core.Exceptions.NillableNotAllowedException(">>CollateralClaim>CollateralClaimTypeDescription");
                }
                clazz.CollateralClaimTypeDescription = new global::Producers.Kis.Xtee.Riik.Ee.Producer.Kis.NonEmptyString(Reader.ReadElementString("CollateralClaimTypeDescription"));
            }
        }
    }
}
